<?php

namespace app\tools;

class Tools
{
    /**
     * 获取日期时间
     * @return string 返回示例: 2023-10-26 23:45:34
     */
    public static function datetime(): string
    {
        return date("Y-m-d H:i:s");
    }

    /**
     * 获取日期
     * @return string 返回示例: 2023-10-26
     */
    public static function date(): string
    {
        return date("Y-m-d");
    }

    /**
     * 获取时间戳
     * @return int 返回示例: 1698335240
     */
    public static function timestamp(): int
    {
        return time();
    }


    /**
     * 获取uuid4
     * @return string 返回示例: ca112f3c-fdf4-4bd5-9a58-38f6245c0d5a
     * @throws \Exception
     */
    public static function uuid4(): string
    {
        if (function_exists('random_bytes')) {
            $data = random_bytes(16);
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
            $data = openssl_random_pseudo_bytes(16);
        } else {
            $data = md5(uniqid(mt_rand(), true), true);
        }

        assert(strlen($data) == 16);

        $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10

        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

    /**
     * 获取uuid4大写
     * @return string 返回示例: 15A316CE-C5E6-4D42-B281-AE6074D695F0
     * @throws \Exception
     */
    public static function uuid4Upper(): string
    {
        return strtoupper(self::uuid4());
    }

    /**
     * 获取uid
     * @return string 返回示例: 30e13491a36841be9c1b60927dd201e5
     * @throws \Exception
     */
    public static function uid(): string
    {
        return str_replace("-", "", self::uuid4());
    }

    /**
     * 获取uid大写
     * @return string 返回示例: C0DCAD47512D44DFA475F880AD3DA9650.005326s
     * @throws \Exception
     */
    public static function uidUpper(): string
    {
        return strtoupper(self::uid());
    }
}
